<?php

namespace app\admin\server\user;

use app\admin\model\music\MusicDjModel;
use app\admin\model\music\MusicModel;
use app\admin\model\music\MusicSkewerModel;
use app\admin\model\user\UserMusicApplyModel;
use app\admin\ServerBase;
use think\facade\Db;
use think\Request;

class UserMusicApplyServer extends ServerBase
{
    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new UserMusicApplyModel();
        $this->musicModel =new MusicModel();
        $this->musicSkewer = new MusicSkewerModel();
        $this->musicDjModel = new MusicDjModel();
    }
    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }
    function getDetail(){
        $info = $this->model->getFindById($this->request->param('id'));
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
    function audit(){
        $id = $this->param['id']??0;
        $status = $this->param['audit']??10;
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if($info['audit'] != 10){
            $this->ERROR='该状态不能审核';
            return false;
        }
        $update['remark'] = $this->param['remark']??'';
        if($status == 20){//审核通过
            $update['audit'] = 20;
        }else{//审核失败
            $update['audit'] = -10;
        }

        Db::startTrans();
        try {
            //至少都会增加一条单曲
            $music=[
                'user_id'=>$info['user_id'],
                'title'=>$info['title'],
                'type_id'=>0,
                'bpm'=>$info['bpm'],
                'key'=>$info['key'],
                'price'=>$info['price'],
                'img'=>$info['img'],
                'url'=>$info['url'],
                'content'=>'',
            ];
            $this->musicModel->save($music);
            $music_id = $this->musicModel->id;
            if($info['type'] == 50){//如果是套曲，则额外在添加套曲
                $skewer=[
                    'title'=>$music['title'],
                    'music_id'=>$music_id,
                    'type_id'=>$info['type_id'],
                    'img'=>$music['img'],
                    'status'=>1,
                    'price'=>$music['price'],
                    'content'=>$music['content'],
                ];
                $this->musicSkewer->save($skewer);
            }elseif($info['type'] == 20){//如果是DJ，则额外添加DJ
                $dj=[
                    'title'=>$music['title'],
                    'music_id'=>$music_id,
                    'type_id'=>$info['type_id'],
                    'img'=>$music['img'],
                    'status'=>1,
                    'price'=>$music['price'],
                    'content'=>$music['content'],
                    'bpm'=>$info['bpm'],
                    'key'=>$info['key'],
                ];
                $this->musicDjModel->save($dj);
            }
            $info->save($update);
            Db::commit();
            return true;
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }

    }
}